package cn.iocoder.yudao.module.crm.convert;

import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelListDataCovert implements Converter<List<String>> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return List.class;
    }

    /**
     * Back to object enum in excel
     *
     * @return Support for {@link CellDataTypeEnum}
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * Convert excel objects to Java objects
     *
     * @param cellData            Excel cell data.NotNull.
     * @param contentProperty     Content property.Nullable.
     * @param globalConfiguration Global configuration.NotNull.
     * @return Data to put into a Java object
     * @throws Exception Exception.
     */
    public List<String> convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                GlobalConfiguration globalConfiguration) throws Exception {
        // 使用字典解析
        String type = getType(contentProperty);
        String label = cellData.getStringValue();
        String[] labels = label.split(",");
        List<String> valueList = new ArrayList<>();
        for(String str : labels){
            String value = DictFrameworkUtils.parseDictDataValue(type, str);
            if (value == null) {
                log.error("[convertToJavaData][type({}) 解析不掉 label({})]", type, str);
            }else{
                valueList.add(value);
            }
        }
        return valueList;
    }

    /**
     * Convert Java objects to excel objects
     *
     * @param value               Java Data.NotNull.
     * @param contentProperty     Content property.Nullable.
     * @param globalConfiguration Global configuration.NotNull.
     * @return Data to put into a Excel
     * @throws Exception Exception.
     */
    public WriteCellData<?> convertToExcelData(List<String> value, ExcelContentProperty contentProperty,
                                                GlobalConfiguration globalConfiguration) throws Exception {
        List<String> labelArray = new ArrayList<>();
        String type = getType(contentProperty);
        for(String str : value){
            String label = DictFrameworkUtils.parseDictDataLabel(type, str);
            if (label != null) {
                labelArray.add(label);
            }
        }
        String data = String.join(",", labelArray);
        return new WriteCellData(data);
    }

    private static String getType(ExcelContentProperty contentProperty) {
        return contentProperty.getField().getAnnotation(DictFormat.class).value();
    }
}
